package line;

import graphEd.Infos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;
import java.util.TreeSet;

import javax.swing.JComboBox;

import com.mxgraph.util.mxResources;

import traitement.TraitementApercu;
import DataStorage.InformationLigneDAOHibernate;
import HibernateSQLite.java.model.InformationLigne;

/**
 * Classe qui permet de choisir une ligne.
 * @see DialogLineChooser
 */
public class LineChooser {
	protected JComboBox tOrgane, tDiversite, tSite, tNumLigne, tAnnee;
	protected String currentOrgane, currentDiversite, currentSite, currentNumLigne, currentAnnee;
	protected InformationLigneDAOHibernate infoLigne;
	//private InformationLigne currentInfoLigne;
	private TraitementApercu apercu;
	private Infos infos = Infos.getInstance();
	
	/*public LineChooser(JComboBox tOrgane, JComboBox tDiversite, JComboBox tSite, JComboBox tNumLigne){
		this(tOrgane, tDiversite, tSite, tNumLigne, null);
	}
	*/
	
	public LineChooser(JComboBox tOrgane, JComboBox tDiversite, JComboBox tSite, JComboBox tNumLigne, JComboBox tAnnee){
		this.tOrgane = tOrgane;
		this.tDiversite = tDiversite;
		this.tSite = tSite;
		this.tNumLigne = tNumLigne;
		this.tAnnee = tAnnee;
		infoLigne = new InformationLigneDAOHibernate();
		init();
		installListeners();
	}

	
	/*protected void before(){
		
	}*/
	
	private void init() {
		//before();
		// Le TreeSet est n�cessaire pour trier les organes
		Set<String> organes = new TreeSet<String>();

		for (InformationLigne info : infoLigne.getListInformationLigne()) {
			//String organe = info.getOrgane();
			//System.out.println(">" + organe + "\t>" + info.getDiversite());
			//organes.add(organe.trim());*/
			organes.add(info.getOrgane().trim());
		}

		tOrgane.addItem(mxResources.get("selectComponent"));
		
		for (String s : organes)
			tOrgane.addItem(s);
		
		apercu = new TraitementApercu(null);
	}

	protected void setInfos(){
		infos.setEssieu((String) tOrgane.getSelectedItem());
		infos.setDiversite((String) tDiversite.getSelectedItem());
		infos.setSite((String)tSite.getSelectedItem());
		infos.setNumLigne((String)tNumLigne.getSelectedItem());
		infos.setAnnee((String) tAnnee.getSelectedItem());
	}
	
	protected void installListeners(){
		tOrgane.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				Set<String> diversites = new TreeSet<String>();
				currentOrgane = (String)tOrgane.getSelectedItem();
				tDiversite.removeAllItems();
				for (InformationLigne info : infoLigne
						.getListInformationLigne(currentOrgane.trim())) {
					System.out.println("diversit�:" + info.getDiversite());
					diversites.add(info.getDiversite().trim());
				}
				for(String s:diversites){
					tDiversite.addItem(s);
				}
				setInfos();
				updateSynoptic();
			}


		});
		
		
		
		tDiversite.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				Set<String> sites = new TreeSet<String>();
				currentDiversite = (String)tDiversite.getSelectedItem();
				tSite.removeAllItems();
				for (InformationLigne info : infoLigne.getListInformationLigne(
						currentOrgane, currentDiversite))
					sites.add(info.getSite().trim());
				
				for(String s:sites){
					tSite.addItem(s);
				}
				setInfos();
				updateSynoptic();
			}
		});
		
		tSite.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				Set<String> numLignes = new TreeSet<String>();
				currentSite = (String)tSite.getSelectedItem();
				tNumLigne.removeAllItems();
				for (InformationLigne info : infoLigne.getListInformationLigne(
						currentOrgane, currentDiversite, currentSite))
					numLignes.add(info.getN_ligne().trim());
				
				for(String s:numLignes){
					tNumLigne.addItem(s);
				}
				setInfos();
				updateSynoptic();
			}
		});
		
		tNumLigne.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				Set<String> annees = new TreeSet<String>();
				currentNumLigne = (String)tNumLigne.getSelectedItem();
				tAnnee.removeAllItems();
				for (InformationLigne info : infoLigne.getListInformationLigne(
						currentOrgane, currentDiversite, currentSite))
					annees.add(info.getAnnee().trim());
				
				for(String s:annees){
					tAnnee.addItem(s);
				}
				setInfos();
				updateSynoptic();
			}
		});
		
		
		tAnnee.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent evt) {
				currentAnnee = (String)tAnnee.getSelectedItem();
				setInfos();
				updateSynoptic();
			}
		});

	}
	
	public void updateSynoptic(){
		System.out.println("update synoptic:"+infos.getSynopticFilename());
		apercu.updateSynoptic(infos.getSynopticFilename());
		//apercu.updateSynoptic(infos.getDataLine());
		//apercu.updateSynoptic("G:/"+tOrgane.getSelectedItem()+"-"+tDiversite.getSelectedItem()+"-"+tSite.getSelectedItem()+"-"+tNumLigne.getSelectedItem()+".png");
	}
	
	public TraitementApercu getApercu(){
		return apercu;
	}
	
	public InformationLigne getCurrentInfoLigne(){
		//if (tOrgane.getSelectedItem()!=null && tDiversite.getSelectedItem()!=null && tSite.getSelectedItem()!=null && tNumLigne.getSelectedItem()!=null)
		return infoLigne.getInformationLigne(
				(String) tOrgane.getSelectedItem(),
				(String) tDiversite.getSelectedItem(),
				(String) tSite.getSelectedItem(),
				(String) tNumLigne.getSelectedItem(),
				(String) tAnnee.getSelectedItem());
	}
	
	public long getInfoLigneId(){
		return getCurrentInfoLigne().getInformationLigne_id();
	}

}
